//Copyright (C) 2012 Chirila Alexandru
//This file is part of Mirror FTP
//
//Mirror FTP is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//Mirror FTP is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with Mirror FTP.  If not, see <http://www.gnu.org/licenses/>.

#ifndef CONN_DTA_H_
#define CONN_DTA_H_

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

#include <fstream>
//#include <iostream>
//#include <string>

#include <pthread.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

#include <netdb.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "model.h"

using namespace std;
/**
 * Class that manages the data connection of the ftp server
 * by receiving files from server in two possible ways:
 * active and passive
 */
class conn_dta{
private:
	bool stop;			///<flag for stopping the download

	int sockfd;			///<socket
	int portno;			///<port number of current connection

	struct sockaddr_in serv_addr;
	unsigned long int thread; ///<thread id
public:
	conn_dta();
	~conn_dta();
public:
	static void *timer (void *arg);
	int get_port(){return portno;};

	int establish_active(struct hostent server);
	int establish_passive(string p);
	int listen_active(model*,long long,string,char);
	int listen_passive(model*,long long,string,char);
	int socket_read(model *m,long long size,int,string,char);

	int closeC();
	void cancel(){stop=1;};
};


#endif /* CONN_DTA_H_ */
